<?php

namespace App\Http\Requests;

use App\Util\Result;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class AgentRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'tel'  => ['bail','required','digits:11','regex:/^1[3|4|5|7|8][0-9]{9}$/']
        ];
    }

    /**
     * 获取已定义验证规则的错误消息。
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => '请填写姓名',
            'tel.required'  => '请填写手机号',
            'tel.digits'    => '请输入有效的手机号',
            'tel.regex'     => '请输入有效的手机号',
        ];
    }

    /**
     * {@inheritdoc}
     */
    protected function formatErrors(Validator $validator)
    {
        return Result::failure( $validator->errors()->first() )->getResult();
    }
}
